package com.cmc.cloud.cmclink.doc.service.impl;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.DoInfoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.service.DoInfoService;
import com.cmc.cloud.cmclink.doc.vo.doinfovo.DoInfoCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.doinfovo.DoInfoPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.doinfovo.DoInfoUpdateReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;

import java.util.*;
import com.cmc.cloud.cmclink.doc.entity.DoInfoDO;
import com.cmc.cloud.cmclink.doc.convert.DoInfoConvert;
import com.cmc.cloud.cmclink.doc.mapper.DoInfoMapper;


/**
 * 进口单证do Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class DoInfoServiceImpl implements DoInfoService {

    @Resource
    private DoInfoMapper doInfoMapper;

    @Override
    public void createDoInfo(DoInfoCreateReqVO createReqVO) {
        // 插入
        DoInfoDO doInfo = DoInfoConvert.INSTANCE.convert(createReqVO);
        doInfoMapper.insert(doInfo);
    }

    @Override
    public void updateDoInfo(DoInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateDoInfoExists(updateReqVO.getId());
        // 更新
        DoInfoDO updateObj = DoInfoConvert.INSTANCE.convert(updateReqVO);
        doInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteDoInfo(Long id) {
        // 校验存在
        validateDoInfoExists(id);
        // 删除
        doInfoMapper.deleteById(id);
    }

    private void validateDoInfoExists(Long id) {
        if (doInfoMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(DoInfoErrorCodeConstants.DO_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DoInfoDO getDoInfo(Long id) {
        return doInfoMapper.selectById(id);
    }

    @Override
    public List<DoInfoDO> getDoInfoList(Collection<Long> ids) {
        return doInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DoInfoDO> getDoInfoPage(DoInfoPageReqVO reqVO) {
        IPage<DoInfoDO> page = Page.of(reqVO.getPageNo(), reqVO.getPageSize());
        
        IPage<DoInfoDO> result = doInfoMapper.selectPageList(page, reqVO);
        return new PageResult<DoInfoDO>(result.getRecords(), result.getTotal());
    }
}
